#include "widget.h"
#include "ui_widget.h"
#include <QTimer>
#include <QPushButton>
#include <QDateTime>
#include <QDebug>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 设置lcd的值
    ui->lcdNumber->display(10);
    // 启动定时器，单位是毫秒
    _timerId = startTimer(1000);

    ui->lcdNumber_2->display(12);
    QTimer* timer = new QTimer(this);
    timer->start(1000);
    connect(timer,&QTimer::timeout,this,[=](){
        int val = ui->lcdNumber_2->intValue() - 1;
        if(val < 0)
            return;
        ui->lcdNumber_2->display(val);
    });

    QTimer* timer2 = new QTimer(this);

    connect(ui->pushButton,&QPushButton::clicked,this,[=](){
        qDebug() << "pushButton";
        timer2->start(1000);
    });
    connect(ui->pushButton_2,&QPushButton::clicked,this,[=](){
        qDebug() << "pushButton_2";
        timer2->stop();
    });

    connect(timer2,&QTimer::timeout,this,&Widget::TimeUpDate);

}

void Widget::TimeUpDate()
{
    QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    ui->label->setText(time);
}



void Widget::timerEvent(QTimerEvent *event)
{
    // 通过定时器标识符 确认是我们的定时器
    if(event->timerId() == _timerId)
    {
        int val = ui->lcdNumber->intValue() - 1;
        if(val < 0)
            return;
        ui->lcdNumber->display(val);

    }
}

Widget::~Widget()
{
    delete ui;
}



